package com.sun.services.gateway.users;


import com.sun.commons.base.R;
import com.sun.dto.users.LoginDto;
import com.sun.dto.users.UserDto;
import com.sun.dto.users.UserSearchModel;
import com.sun.services.users.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * MP自动填充Handler
 *
 * @Author sun
 * @CreateTime 2025/9/6 23:40
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户模块")
@AllArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public R login(@RequestBody LoginDto loginDto) {
        return userService.Login(loginDto);
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public R register(@RequestBody UserDto loginDto) {
        return userService.register(loginDto);
    }

    @PreAuthorize("hasRole('admin') or hasAuthority('user:update')")

    @PostMapping("/update")
    @Operation(summary = "用户修改")
    public R update(@RequestBody UserDto loginDto) {
        return userService.update(loginDto);
    }

    @PostMapping("/delete")
    @Operation(summary = "用户删除")
    public R delete(@RequestBody Long[] ids) {
        return userService.delete(ids);
    }

    @PostMapping("/updatePassword")
    @Operation(summary = "用户修改密码")
    public R updatePassword(@RequestBody UserDto loginDto) {
        return userService.updatePassword(loginDto);
    }

    @PostMapping("/resetPassword")
    @Operation(summary = "用户重置密码")
    public R resetPassword(@RequestBody UserDto loginDto) {
        return userService.resetPassword(loginDto);
    }
    @GetMapping("/getUserList")
    @Operation(summary = "用户列表")

    public R getUserPageList( UserSearchModel searchModel) {
        return R.ok().setData(userService.getUserListByPage(searchModel));
    }


}
